package com.slz3.business.module.configManagement.conCircle.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conCircle.domain.ConCircle;
import com.slz3.business.module.configManagement.conCircle.domain.ConCircleFactory;
import com.slz3.business.module.configManagement.conCircle.steward.ConCircleSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConCircleServiceImpl implements ConCircleService {

    @Override
    public ConCircle save(ConCircle conCircle) {
        return Repositorys.conCircle.save(ConCircleFactory.initBaseFields(conCircle));
    }

    @Override
    public Page<ConCircle> page(MultiValueMap<String, String> params) {
        Page<ConCircle> conCirclePage = Repositorys.conCircle.findAll(
                ConCircleSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conCirclePage;
    }

    @Override
    public ConCircle modify(ConCircle conCircleNew) {
        return Repositorys.conCircle.saveAndFlush(ConCircleFactory.initBaseFields(conCircleNew));
    }

    @Override
    public Optional<ConCircle> single(UUID uuid) {
        return Repositorys.conCircle.findById(uuid);
    }

    @Override
    public List<ConCircle> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conCircle.findAllById(uuids);
    }

    @Override
    public List<ConCircle> delete(List<ConCircle> conCircleList) {
        conCircleList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConCircleFactory.initBaseFields(item);
        });
        return Services.conCircleTransactional.batchModify(conCircleList);
    }

}
